package com.muki.spider.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * 用以缓存已经爬取过得uri
 * @author ljh_2017
 *
 */
public class SpiderUriMemoryCache {

	public static final Map<String,Map<String,Boolean>> uris = new HashMap<>();
	private final static Logger log = Logger.getLogger(SpiderUriMemoryCache.class);
	/**
	 * 将uri推送到指定集合中
	 * @param topic
	 * @param uri
	 */
	public static void put(String topic,String uri) {
		Map<String,Boolean> results = uris.getOrDefault(topic, new HashMap<>());
		results.put(uri, true);
		uris.put(topic, results);
	}

	//判断是否重复
	/**
	 * 判重
	 * @param topic
	 * @param uri
	 * @return
	 */
	public static boolean isRepetition(String topic,String uri) {
		Map<String,Boolean> results = uris.getOrDefault(topic, null);
		if(results==null) {
			return false;
		}
		return results.getOrDefault(uri,false);
	}
	
	/**
	 * 去重机制
	 * 如果重复则不操作
	 * 否则存入缓存中
	 * @param topic
	 * @param uri
	 */
	public static boolean putIfAbsent(String topic,String uri) {
		if(!isRepetition(topic, uri)) {
			put(topic, uri);
			return true;
		} 
		return false;
	}
	
	/**
	 * 设置持久化
	 * @param persistence
	 */
	public static void setPersistence(OverUriPersistence persistence,String key,List<String> value) {
		persistence.persistence(key,value);
	}

}
